<?php
$form = new jQuickForm('ajaxHierselect');
$form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
    'syncHS'  => array(2, 16),
    'asyncHS' => array(2, 16)
)));
jQuickForm::js()->addJs('/vendors/jQuickForm/quickform-hierselect.js');
//jaguar_debug(dirname(__FILE__));exit;
include($_SERVER['DOCUMENT_ROOT'].'/cms/ext/hierselect-loader.php');
$loader = new OptionLoader();

$fsSync = $form->addElement('fieldset')->setLabel('Sync call');

$fsSync->addElement('hierselect', 'syncHS', array('style' => 'width: 250px;'))
       ->setLabel('Choose package:')
       ->loadOptions(array($loader->getOptions(), array()),
                     array($loader, 'getOptions'), 'loadOptionsSync')
       ->setSeparator('<br />');

$fsAsync = $form->addElement('fieldset')->setLabel('Async call');
$fsAsync->addElement('hierselect', 'asyncHS', array('style' => 'width: 250px;'))
        ->setLabel('Choose package again:')
        ->loadOptions(array($loader->getOptions(), array()),
                      array($loader, 'getOptions'), 'loadOptionsAsync')
        ->setSeparator('<br />');

$form->addElement('submit', 'testSubmit', array('value' => 'Send'));

// output script tags for AJAX server
$ajaxHelper = new HTML_AJAX_Helper();
$ajaxHelper->serverUrl = '/cms/ext/hierselect-server.php';
$ajaxHelper->stubs[]   = 'OptionLoader';
Jaguar::view()->setVar('body',$ajaxHelper->setupAJAX());
jQuickForm::js()->addJsInline('
// <![CDATA[

function loadOptionsSync(keys)
{
    return (new OptionLoader).getOptionsAjax(keys);
}

function loadOptionsAsync(keys, selectId)
{
    // trying to load options for an empty value (or waiting for an async
    // call result)
    if (\'\' == keys[keys.length - 1]) {
        return qf.elements.hierselect.missingOptions;
    }

    var callback = {
        getOptionsAjax: qf.elements.hierselect.getAsyncCallback(selectId, keys)
    };

    // Asynchronous call
    (new OptionLoader(callback)).getOptionsAjax(keys);

    // actual options will be added later, for now just return empty options
    // (this will also prevent useless AJAX requests)
    return qf.elements.hierselect.missingOptions;
}

// ]]>
');
?>